package
{
	import com.bit101.components.HSlider;
	import com.bit101.components.ImageRadioBtn;
	import com.bit101.components.ImageSlider;
	import com.bit101.components.PushButton;
	import com.bit101.components.RadioButton;
	import com.bit101.components.Slider;
	import com.liu.load.LoadInfo;
	import com.liu.load.LoadManager;
	
	import events.AddTxtEvent;
	import events.BitmapEvent;
	import events.CategoryEvent;
	import events.ColorEvent;
	import events.DataObjEvent;
	import events.EditEvent;
	import events.NumEvent;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.ByteArray;
	
	import models.ModelLocator;
	
	import utils.Coursor;
	
	import view.AlertShow;
	import view.BottomSprite;
	import view.CategoryBtns;
	import view.ColorChoose;
	import view.Console;
	import view.EditSpace;
	import view.EditTxt;
	import view.HCanvas;
	import view.ListItem;
	import view.PricePanle;
	import view.SizePanle;
	import view.TabNav;
	import view.TextControl;
	import view.TextSprite;
	import view.TotalPanle;
	import view.UploadAlert;
	import view.UploadImg;
	
	[SWF(height="1000",width="1016",backgroundColor="#e5e5e5")]
	public class Main extends Sprite
	{
		private var categoryBtns:CategoryBtns;
		private var editSpace:EditSpace;
		private var colorchoose:ColorChoose;
		private var listItem:ListItem;
		private var pricePanle:PricePanle;
		private var totalPanle:TotalPanle;
		private var sizePanle:SizePanle;
		
		private var resultList:Array;
		private var tprice:int;
		private var typeAry:Array;
		public var editTxt:EditTxt;
		public var alert:AlertShow = new AlertShow;
		public var uploadImg:UploadImg;
		private var model:ModelLocator = ModelLocator.getInstance();
		private var bottom:BottomSprite;
		public function Main()
		{
			Coursor.getInstance().container = this;
			
			var loadinfo:LoadInfo = new LoadInfo("assets/config/config.xml",LoadInfo.XML,initLoad);
			LoadManager.getInstance().addSingleLoad(loadinfo);
			
			//new PushButton(this,0,0,"生成",onCreatString);
			ExternalInterface.addCallback("sendToserver",onCreatString);
		}
		private function onCreat(event:MouseEvent):void{
			var byte:ByteArray = editSpace.currentEdit.getAllInfo();
			var str:String = new String;
			byte.position = 0;
			for(var i:int;i<byte.length;i++){
				str += byte.readByte() + ",";
			}
			var file:FileReference = new FileReference;
			file.save(byte,'123.txt');
			//var bitmap:Bitmap = new Bitmap(editSpace.currentEdit.drawBitmap());
			//this.addChild(bitmap);
		}
		private function onCreatString(event:Event):void{
			//ExternalInterface.call("go");
			//return;
			var urlreq:URLRequest = new URLRequest();
			var urlvar:URLVariables = new URLVariables();
			urlvar.color = model.color;
			
			var typenum:String = '';
			if(model.typePriceAry){
				var i:int=0;
				for each(var obj:Object in model.typePriceAry){
					if(obj.hasOwnProperty("num") && obj.num != 0){
						typenum += encodeURI(obj.name) + "^" + obj.num + "|"
						i++;
					}
				}
				if(i == 0){
					alertShow("您还没有选择数量");
					return;
				}else{
					typenum = typenum.substr(0,typenum.length-1);
					urlvar.typenum = typenum;
				}
			}else{
				alertShow("您还没有选择数量");
				return;
			}
			if(!model.sizeid){
				alertShow("您还没有选择大小");
				return;
			}
			var str:String = editSpace.getAllInfo();
			if(str == ""){
				alertShow("您至少要选择一个打印面");
				return;
			}
			urlvar.size = model.sizeid;
			urlvar.items = str;
			urlvar.id = 1;
			urlvar.price = totalPanle.resultPrice;
			
			urlreq.data = urlvar;
			urlreq.url = model.submitInfo;
			urlreq.method=URLRequestMethod.POST
			
			var urlloader:URLLoader = new URLLoader();
			urlloader.addEventListener(Event.COMPLETE,onSubmitInfo);
			urlloader.load(urlreq);
			
			
			//writeFile(urlvar)
			
			//var ary:Array = editSpace.getAllInfoImg();
			//upload();
			
		}
		private var uploadKey:String
		private function onSubmitInfo(event:Event):void{
			trace(event.target.data);
			uploadKey = event.target.data;
			upload();
		}
		public function upload():void{
			uploadImg = new UploadImg(this);
			uploadImg.upLoad(editSpace.getAllInfoImg(),this.uploadKey);
		}
		public function writeImg(byte:ByteArray):void{
			var file:FileReference = new FileReference;
			file.save(byte,'result.png');
		}
		public function alertShow(str:String):void{
			alert.setInfo(str,nullFun);
			this.addChild(alert);
			alert.x = 200;
			alert.y = 200;
		}
		private function nullFun():void{
			
		}
		private function writeFile(urlvar:URLVariables):void{
			var str:String = "<info><id>1</id><color>" + urlvar.color + "</color><typenum>" + 
								urlvar.typenum + "</typenum><size>" + urlvar.size + "</size><items>" + 
								urlvar.items + "</items></info>";
			
			var newstr:String = "<result>";
			for(var i:int=0;i<5;i++){
				str = "<info><id>" + i + "</id><color>" + urlvar.color + "</color><typenum>" + 
					urlvar.typenum + "</typenum><size>" + urlvar.size + "</size><items>" + 
					urlvar.items + "</items></info>";
				newstr += str;
			}
			newstr += "</result>";
			var file:FileReference = new FileReference;
			var xml:XML = new XML(newstr);
			file.save(xml.toString(),'result.xml');
		}
		private function initLoad(xml:XML):void{
			model.setUrl(xml);
			
			editSpace = new EditSpace;
			editSpace.x = 170;
			editSpace.y = 60;
			this.addChild(editSpace);
			
			var tab:TabNav = new TabNav(model.tabUrl)
			tab.x = 620;
			tab.y = 60;
			this.addChild(tab);
			tab.addEventListener(BitmapEvent.BITMAPEVENT,onBitmapAdd);
			
			editTxt = new EditTxt("assets/config/font.txt");
			this.addChild(editTxt);
			editTxt.visible = false;
			
			resultList = new Array;
			
			colorchoose = new ColorChoose();
			//colorchoose.source = xml;
			this.addChild(colorchoose);
			colorchoose.addEventListener(ColorEvent.COLOREVENT,onColorSelect);
			
			listItem = new ListItem(this,633,330)
			listItem.addEventListener(AddTxtEvent.ADDTXTEVENT,addTxt);
			listItem.addEventListener(BitmapEvent.BITMAPEVENT,onBitmapAdd);
			listItem.addEventListener(DataObjEvent.DATAEVENT,onSaveItem);
			listItem.addEventListener(EditEvent.EDITEVENT,onedit);
			editSpace.listItem  = listItem;
			
			pricePanle = new PricePanle();
			//pricePanle.source = xml;
			this.addChild(pricePanle);
			pricePanle.addEventListener(NumEvent.NUMEVENT,onNum);
			
			totalPanle = new TotalPanle;
			this.addChild(totalPanle);
			
			categoryBtns = new CategoryBtns(model.btnUrl);
			this.addChild(categoryBtns);
			categoryBtns.addEventListener(CategoryEvent.CATEGORYEVENT,onNavigation);
			
			addline();
			
			bottom = new BottomSprite;
			this.addChild(bottom);
			bottom.y = 910;
			bottom.x = 150;
			bottom.addEventListener(Event.SELECT,onCreatString);
		}
		private function onNavigation(event:CategoryEvent):void{
			//model.init = false;
			//var url:String = model.detailUrl + "?id=" +event.data.id;
			var url:String = "assets/config/detail" + event.data.id + ".xml";
			var loadinfo:LoadInfo = new LoadInfo(url,LoadInfo.XML,itemConfig);
			LoadManager.getInstance().addSingleLoad(loadinfo);
		}
		public function clear():void{
			editSpace.clear();
			listItem.clear();
			resultList = new Array;
			onNum();
		}
		private function itemConfig(xml:XML):void{
			resultList = new Array;
			onNum();
			//editSpace.clearType();
			//model.init = true;
			//colorchoose = new ColorChoose();
			colorchoose.source = xml;
			//this.addChild(colorchoose);
			//colorchoose.addEventListener(ColorEvent.COLOREVENT,onColorSelect);
			
			//listItem = new ListItem(this,625,300)
			listItem.source = xml;
			//listItem.addEventListener(AddTxtEvent.ADDTXTEVENT,addTxt);
			//listItem.addEventListener(BitmapEvent.BITMAPEVENT,onBitmapAdd);
			//listItem.addEventListener(DataObjEvent.DATAEVENT,onSaveItem);
			//listItem.addEventListener(EditEvent.EDITEVENT,onedit);
			
			//pricePanle = new PricePanle();
			pricePanle.source = xml;
			//this.addChild(pricePanle);
			//pricePanle.addEventListener(NumEvent.NUMEVENT,onNum);
			
			/*totalPanle = new TotalPanle;
			this.addChild(totalPanle);*/
			
			if(sizePanle){
				if(sizePanle.url != String(xml.size.size.@url)){
					if(sizePanle.parent){
						sizePanle.parent.removeChild(sizePanle);
					}
					sizePanle = new SizePanle(xml.size.size.@url);
					this.addChild(sizePanle);
				}
			}else{
				sizePanle = new SizePanle(xml.size.size.@url);
				this.addChild(sizePanle);
			}
		}
		private function onNum(event:NumEvent = null):void{
			if(event){
				tprice = event.num;
				typeAry = event.ary;
			}else{
				tprice = 0;
				typeAry = new Array;
			}
			model.typePriceAry = typeAry;
			var num:int;
			for(var i:int;i<typeAry.length;i++){
				if(typeAry[i].hasOwnProperty("num")){
					num += typeAry[i].num;
				}
			}
			totalPanle.setSource(resultList,tprice,num);
		}
		private var editMark:String;
		private var editColor:String;
		private function onedit(event:EditEvent):void{
			editMark = event.data.mark;
			editSpace.setMarkColor(editMark,editColor,event.showType,event.data);
			editSpace.drawRct(event.data);
		}
		private function onColorSelect(event:ColorEvent):void{
			editColor = event.color;
			model.color = editColor;
			editSpace.setMarkColor(editMark,editColor);
		}
		private function addTxt(event:AddTxtEvent):void{
			var console:Console = editSpace.addTxt();
			if(console){
				editTxt.visible = true;
				editTxt.x = 200;
				editTxt.y = 300;
				editTxt.console = console;
			}
		}
		private function onBitmapAdd(event:BitmapEvent):void{
			editSpace.addBitmap(event.bitmap,event.url,event.scale,event.levle);
		}
		[Embed(source="assets/components/lineOne.png")]
		private var lineOneCls:Class;
		[Embed(source="assets/components/lineTwo.png")]
		private var lineTwoCls:Class;
		public function addline():void{
			var sp:Sprite = new Sprite;
			this.addChild(sp);
			
			var txt:TextField = new TextField;
			txt.width = 400;
			txt.height = 22;
			txt.htmlText = "<font color='#656565' size='12'>点亮眼镜的才能打印在衣服上</font>";
			txt.x = 625;
			txt.y = 290;
			txt.selectable = false;
			sp.addChild(txt);
			
			var bitmapOne:Bitmap = new lineOneCls;
			sp.addChild(bitmapOne);
			bitmapOne.x = 625;
			bitmapOne.y = 315;
			
			var bitmapTwo:Bitmap = new lineTwoCls;
			sp.addChild(bitmapTwo);
			bitmapTwo.x = 120;
			bitmapTwo.y = 690;
			
		}
		private function onSaveItem(event:DataObjEvent):void{
			var data:Object = event.data;
			if(event.add){
				if(resultList.indexOf(data) == -1){
					resultList.push(data);
				}
			}else{
				var index:int = resultList.indexOf(data);
				if(index != -1){
					resultList.splice(index,1);
				}
			}
			
			if(typeAry == null){
				return;
			}
			var num:int;
			for(var i:int;i<typeAry.length;i++){
				if(typeAry[i].hasOwnProperty("num")){
					num += typeAry[i].num;
				}
			}
			totalPanle.setSource(resultList,tprice,num);
		}
		
	}
}